package com.mantisframework.core.spring.ioc;

import javax.servlet.ServletContext;

import jodd.util.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
 * 
 *
 * @author madong
 * @date Dec 23, 2012 4:55:54 PM
 */
public class SpringIocUtil {
	private static String fail_info = "get %s  fail,ServletContext  not loading.";
	private static Logger log = Logger.getLogger(SpringIocUtil.class);
	private static String springConfigFilePath = "applicationContext.xml";
	private static String success_info = "get %s   success:";

	public static synchronized <T> T getBean(Class<T> beanName) {

		return getInstence(beanName, getContext());
	}

	public static synchronized <T> T getBean(Class<T> beanName,
			ApplicationContext context) {

		return getInstence(beanName, context);
	}

	public static synchronized <T> T getBean(Class<T> beanName,
			ServletContext servletcontext) {

		return getInstence(beanName, getContext(servletcontext));
	}

	public static synchronized Object getBean(String beanName) {
		return getInstence(beanName, getContext());
	}

	public static synchronized Object getBean(String beanName,
			ApplicationContext context) {
		return getInstence(beanName, context);
	}

	public static synchronized Object getBean(String beanName,
			ServletContext servletcontext) {
		return getInstence(beanName, getContext(servletcontext));
	}

	private static ApplicationContext getContext() {
		ApplicationContext context = null;
		context = WebApplicationContextUtils
				.getWebApplicationContext(SpringIocContextListener
						.getServletcontext());
		if (context == null) {
			context = initLocalAppContext();
			return context;
		}
		return context;
	}

	private static ApplicationContext getContext(ServletContext servletcontext) {
		ApplicationContext context = null;
		context = WebApplicationContextUtils
				.getWebApplicationContext(servletcontext);
		return context;
	}

	private static <T> T getInstence(Class<T> beanName,
			ApplicationContext context) {
		try {
			if (context == null) {

				log.error(String.format(fail_info, beanName));
				return null;
			}
			T t = context.getBean(beanName);
			if (t != null) {
				log.debug(String.format(success_info, beanName) + t);
				return t;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return null;
	}

	private static Object getInstence(String beanName,
			ApplicationContext context) {
		try {
			if (getContext() == null) {
				log.error(String.format(fail_info, beanName));
				return null;
			}
			Object t = context.getBean(beanName);
			if (t != null) {
				log.debug(String.format(success_info, beanName) + t);
				return t;
			}
		} catch (Exception e) {
			log.error(e);
		}
		return null;
	}

	private static ApplicationContext initLocalAppContext() {
		if(StringUtil.isEmpty(springConfigFilePath)){
			log.error("spring Config FilePath is empty.");
			return null;
		}
		ApplicationContext context = new ClassPathXmlApplicationContext(
				springConfigFilePath);
		log.info("load " + springConfigFilePath + " success!");
		return context;
	}
	public static void setSpringConfigFilePath(String springConfigFilePath){
		springConfigFilePath=springConfigFilePath;
	}

}
